package com.ruoyi.robot.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.robot.pojo.po.Customer;
import com.ruoyi.robot.pojo.dto.CustomerDto;
import com.ruoyi.robot.pojo.dto.LoginDto;
import com.ruoyi.robot.service.CustomerService;
import com.ruoyi.robot.util.BaseUserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xds
 * @since 2023-01-16
 */
@Api(tags = "客户接口")
@RestController
@RequestMapping("/robot/customer")
public class CustomerController extends BaseController {

    private final CustomerService customerService;

    @Autowired
    public CustomerController(CustomerService customerService) {
        this.customerService = customerService;
    }


    /**
     * 查询用户表列表
     */
    @ApiOperation("查询用户表列表")
    @GetMapping("/list")
    public TableDataInfo list(CustomerDto customerDto) {
        startPage();
        List<Customer> list = customerService.selectCustomerList(customerDto);
        return getDataTable(list);
    }

    /**
     * 导出用户表列表
     */
    @ApiOperation("导出用户表列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerDto customerDto) {
        List<Customer> list = customerService.selectCustomerList(customerDto);
        ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class);
        util.exportExcel(response, list, "用户表数据");
    }

    /**
     * 获取用户表详细信息
     */
    @ApiOperation("获取用户表详细信息")
    @GetMapping(value = "/{customerId}")
    public AjaxResult getInfo(@PathVariable("customerId") Long customerId) {
        return AjaxResult.success(customerService.selectCustomerByCustomerId(customerId));
    }

    /**
     * 新增用户表
     */
    @ApiOperation("新增用户表")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CustomerDto customerDto) {
        return customerService.insertCustomer(customerDto);
    }

    /**
     * 修改用户表
     */
    @ApiOperation("修改用户表")
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerDto customerDto) {
        return toAjax(customerService.updateCustomer(customerDto));
    }

    /**
     * 删除指定用户
     *
     * @param customerId
     * @return
     */
    @ApiOperation("删除指定用户")
    @DeleteMapping(value = "customerId")
    public AjaxResult deleteCustomer(@PathVariable("customerId") Long customerId) {
        return toAjax(customerService.deleteCustomerByCustomerId(customerId));
    }

    /**
     * 删除用户表
     */
    @ApiOperation("删除多个用户")
    @DeleteMapping("/{customerIds}")
    public AjaxResult remove(@PathVariable Long[] customerIds) {
        return toAjax(customerService.deleteCustomerByCustomerIds(customerIds));
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginDto loginDto) {
        return customerService.login(loginDto);
    }

    @ApiOperation("获取验证码")
    @PostMapping("/generateImageCode")
    public AjaxResult generateImageCode(HttpServletResponse response) throws IOException {
        return customerService.generateImageCode(response);
    }
}

